﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using GCELibrary.Current;

namespace ZF.WS.Admin.Member
{
 
    public partial class changepwd : ZF.BLL.Page.MMPage
    {
        [PageArgument(Key = "ot", DefaultValue = PageOperType.Edit)]
        public override PageOperType OperType
        {
            get
            {
                return base.OperType;
            }
            set
            {
                base.OperType = value;
            }
        }

        /// <summary>
        /// 1为中介，0为普通
        /// </summary>
        [PageArgument(Key = "m", DefaultValue = 0)]
        public int IsMedium { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Edit();
            }
        }
        public void InitControls()
        {

        }
        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();


            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                    if (member == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    if (member.Pwd != SiteInfo.EncryptBase64(this.tbOPwd.Text))
                    {

                        WarnMessageLink link1 = new WarnMessageLink("原始密码错误");

                        WarnFactory.CreateWarn(link1.ToString(), WarnIcon.Error, WarnLink.GoBack).Alert();
                    }
                    if (this.tbPassword.Text != this.tbPassword1.Text)
                    {
                        WarnMessageLink link1 = new WarnMessageLink("两次输入的密码不相同");

                        WarnFactory.CreateWarn(link1.ToString(), WarnIcon.Error, WarnLink.GoBack).Alert();
                    }
                    member.Pwd = SiteInfo.EncryptBase64(this.tbPassword.Text);


                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = "密码";

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改{0}", msgKey));

                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, "/Person/index.aspx").Alert();

                }


            }

        }


    }
}
